home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / text / cmanual.lzh / ACM3.lzh / AmigaDOS / Example8.c < prev    next >
C/C++ Source or Header  |  1991-01-12  |  3KB  |  109 lines

  1. /* Example8                                                        */
  2. /* This program takes a directory/device name as parameter, and    */
  3. /* prints out all the file/directory-names inside it. This example */
  4. /* describes how to use Examine() and ExNext().                    */
  5.  
  6.  
  7. #include <libraries/dos.h>
  8. #include <exec/memory.h>
  9.  
  10.  
  11. main( argc, argv )
  12. int argc;
  13. char *argv[];
  14. {
  15.   struct FileLock *lock;
  16.   struct FileInfoBlock *fib_ptr; /* Declare a FileInfoBlock */
  17.                                  /* pointer called fib_ptr. */
  18.  
  19.  
  20.   if( argc < 2 )
  21.   {
  22.     /* No directory/device specified! */
  23.     printf("Which directory/device do you actually want to examine?\n");
  24.     exit();
  25.   }
  26.  
  27.  
  28.  
  29.   /* Allocate enough memory for a FileInfoBlock structure: */
  30.   fib_ptr = (struct FileInfoBlock *)
  31.             AllocMem( sizeof( struct FileInfoBlock ),
  32.                       MEMF_PUBLIC | MEMF_CLEAR );
  33.  
  34.   /* Check if we have allocated the memory successfully: */
  35.   if( fib_ptr == NULL )
  36.   {
  37.     printf("Not enough memory!\n");
  38.     exit();
  39.   };
  40.  
  41.  
  42.   
  43.   /* Try to lock the file: */
  44.   lock = (struct FileLock *) Lock( argv[ 1 ], SHARED_LOCK );
  45.   
  46.   /* Colud we lock the file? */
  47.   if( lock == NULL )
  48.   {
  49.     printf("Could not lock the file/directory!\n");
  50.  
  51.     /* Deallocate the memory we have allocated: */
  52.     FreeMem( fib_ptr, sizeof( struct FileInfoBlock ) );
  53.     
  54.     exit();
  55.   }
  56.  
  57.  
  58.  
  59.   /* Try to examine the directory/device/(file): */
  60.   if( Examine( lock, fib_ptr ) )
  61.   {
  62.     /* Check if it is a directory/device: */
  63.     if( fib_ptr->fib_DirEntryType > 0 )
  64.     {
  65.       /* Print out the directory/device name with underlined characters: */
  66.       /* \033[4m : Underline */
  67.       /* \033[0m : Normal    */
  68.       printf("\033[4m%s\033[0m\n", fib_ptr->fib_FileName );
  69.  
  70.  
  71.  
  72.       /* As long as we can examine files/directories we continue: */
  73.       while( ExNext( lock, fib_ptr ) )
  74.       {
  75.         /* If it is a file we print out the name with white characters. */
  76.         /* However, if it is a (sub)directory we use orange:            */
  77.         if( fib_ptr->fib_DirEntryType < 0 )
  78.           printf("%s\n", fib_ptr->fib_FileName ); /* File */
  79.         else
  80.           printf("\033[33m%s\033[31m\n", fib_ptr->fib_FileName ); /* Dir */
  81.  
  82.         /* \033[33m : Orange (Colour 3) */
  83.         /* \033[31m : White  (Colour 1) */
  84.       }
  85.  
  86.  
  87.  
  88.       /* Check what went wrong. If it was not because there were no more */
  89.       /* files in the directory (ERROR_NO_MORE_ENTRIES), something       */
  90.       /* terrible has happened!                                          */
  91.       if( IoErr() != ERROR_NO_MORE_ENTRIES )
  92.         printf("ERROR WHILE READING!!!\n");
  93.     }
  94.     else
  95.       printf("%s is a file!\n", argv[1] );
  96.   }
  97.   else
  98.     printf("Could not examine %s!\n", argv[ 1 ] );
  99.  
  100.  
  101.  
  102.   /* Unlock the file: */
  103.   UnLock( lock );  
  104.  
  105.  
  106.  
  107.   /* Deallocate the memory we have allocated: */
  108.   FreeMem( fib_ptr, sizeof( struct FileInfoBlock ) );
  109. }